//
//  AsiaNewSongTableViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/17.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "AsiaNewSongTableViewCell.h"
#import "SongsTableViewCell.h"
#import "AsiaNewSongModel.h"
#import "CustomerNetworking.h"
#import "AsiaSongDetailTableViewCell.h"
#import <UIImageView+WebCache.h>


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@implementation AsiaNewSongTableViewCell
- (void)dealloc
{
    [_songsPageModel release];
    [_tableView release];
    [_titleLabel release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self loadData];
        
        [self createSubViews];
    }
    return self;
}

-(void)loadData
{
    self.songsArray = [NSMutableArray array];
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        NSArray *songsArray = [NSArray arrayWithArray:[[[response objectForKey:@"data"] objectAtIndex:6] objectForKey:@"data"]];
        for (NSDictionary *dic in songsArray) {
            AsiaNewSongModel *asiaNewSongModel = [AsiaNewSongModel modelWithDictionary:dic];
            [self.songsArray addObject:asiaNewSongModel];
        }
        
        [self.tableView reloadData];
    }];
}

-(void)createSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT_PRO(40), SCREEN_WIDTH, HEIGHT_PRO(80)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    [_tableView release];
    
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), SCREEN_WIDTH, HEIGHT_PRO(20));
}

-(void)setSongsPageModel:(SongsPageModel *)songsPageModel
{
    if (_songsPageModel != songsPageModel) {
        [_songsPageModel release];
        _songsPageModel = [songsPageModel retain];
    }
    
    self.titleLabel.text = songsPageModel.name;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AsiaNewSongModel *asiaNewSongModel = _songsArray[indexPath.row];
    AsiaSongDetailTableViewCell *cell = [AsiaSongDetailTableViewCell asiaSongDetailTableViewCellWith:tableView];
    cell.asiaNewSongModel = asiaNewSongModel;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

+(AsiaNewSongTableViewCell *)AsiaNewSongTableViewCellWith:(UITableView *)tableView
{
    static NSString *AsiaNewIdentify = @"AsiaNewIdentify";
    AsiaNewSongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AsiaNewIdentify];
    if (!cell) {
        cell = [[AsiaNewSongTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AsiaNewIdentify];
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", _songsArray.count);
    return _songsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT_PRO(80);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AsiaNewSongModel *asiaNewSongModel = _songsArray[indexPath.row];
    [self.delegate pushValueToWebView:[asiaNewSongModel.action objectForKey:@"value"]];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
